<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button type="primary" @click="openEdit(false)"> 新增菜单 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: openEdit.bind(null, true, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <MenuEdit ref="editRef" @success="reload" />
  </div>
</template>
<script lang="ts" setup>
  import { nextTick, ref, h } from 'vue';

  import { BasicTable, useTable, BasicColumn, FormSchema, TableAction, StatusTagRender } from '@/components/Table';
  import Icon from '@/components/Icon/Icon.vue';

  import MenuEdit from './MenuEdit.vue';

  import menuApi from '@/api/authority/menuApi';

  import { formatToDateTime } from '@/utils/dateUtil';

  // 自定义组件名称, 默认为文件名
  defineOptions({ name: 'Menu' });

  // 搜索表单
  const searchFormSchema: FormSchema[] = [
    {
      field: 'showName',
      label: '菜单名称',
      component: 'Input',
      colProps: { span: 8 },
    },
    {
      field: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '启用', value: '0' },
          { label: '停用', value: '1' },
        ],
      },
      colProps: { span: 8 },
    },
  ];

  // 表格列
  const columns: BasicColumn[] = [
    {
      title: '菜单名称',
      dataIndex: 'showName',
      width: '10rem',
      align: 'left',
    },
    {
      title: '图标',
      dataIndex: 'icon',
      width: '10rem',
      customRender: ({ record }) => {
        return h(Icon, { icon: record.icon });
      },
    },
    {
      title: '权限标识',
      dataIndex: 'perms',
      width: '10rem',
    },
    {
      title: '组件',
      dataIndex: 'cmpPath',
      width: '10rem',
    },
    {
      title: '排序',
      dataIndex: 'orderNum',
      width: '10rem',
    },
    {
      title: '状态',
      dataIndex: 'status',
      width: '10rem',
      customRender: ({ record }) => StatusTagRender(record.status),
    },
    {
      title: '创建人',
      dataIndex: 'creator',
      width: '10rem',
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: '15rem',
      format: (text) => formatToDateTime(text),
    },
  ];

  // 注册表格
  const [registerTable, { reload, expandAll }] = useTable({
    title: '菜单列表',
    api: menuApi.tree,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    isTreeTable: true,
    pagination: false,
    striped: false,
    useSearchForm: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    showTableSetting: true,
    tableSetting: {
      redo: true, // 刷新
      size: false, // 是否显示调节行高按钮
      setting: true, // 是否显示设置按钮
      fullScreen: true, // 是否显示全屏按钮
      expandAll: true, // 是否显示折叠全部按钮
    },
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  // 编辑窗口
  const editRef = ref<any>({});

  function openEdit(isUpdate: boolean, record?: Recordable) {
    editRef.value.open(isUpdate, record);
  }

  function handleDelete(record: Recordable) {
    console.log(record);
  }

  function onFetchSuccess() {
    // 演示默认展开所有表项
    nextTick(expandAll);
  }
</script>
